package com.lida.study.common;

import org.springframework.boot.system.ApplicationHome;
import org.springframework.util.StringUtils;

/**
 * @author lD
 * @date 2019/7/17
 */
public class Constants {

    /**
     * 项目路径
     */
    public static final String APPLICATION_PATH = getUserDir();

    /**
     * 图片数据存放地址，目前是temp-file根目录
     */
    public static final String TEMP_RESOURCES_DATA_PATH = APPLICATION_PATH + "temp-file/";

    /**
     * 资源路径
     */
    public static final String DATA_PATH = APPLICATION_PATH;

    /**
     * opencv语言包路径
     */
    public static final String TESS_RESOURCES_LANGUAGE_PATH = APPLICATION_PATH + "tessdata";

    private static String getUserDir() {
        String userIdr = new ApplicationHome(Constants.class).getSource().getParentFile().toString();
        if (StringUtils.isEmpty(userIdr)) {
            throw new IllegalArgumentException("System directory retrieval failed");
        }
        // windows系统需要将最前面的"/"去掉
        String osName = System.getProperties().getProperty("os.name");
        if (osName.contains("Windows")) {
            if (userIdr.endsWith("target")) {
                userIdr += "/classes/";
            } else if (userIdr.endsWith("target/") || userIdr.endsWith("target\\")) {
                userIdr += "classes/";
            }
        }
        if (userIdr.endsWith("/") || userIdr.endsWith("\\")) {
            return userIdr;
        } else {
            return userIdr + "/";
        }
    }
}
